﻿using System;
using System.Runtime.Serialization;

namespace CodeArt.SharePoint.Controls
{
    using CodeArt.SharePoint.Configuration;

    [Serializable]
    public class PickerSearchData: ISerializable
    {
        string _lookupField = null;
        string _PickerTemplateName = null;
        private PickerControlElement _defaultPickerTemplate = null;

        public string LookupField
        {
            get {
                if (string.IsNullOrEmpty(_lookupField))
                    _lookupField = PickerTemplate.ViewFields.LookupNameField;
                return _lookupField; 
            }
        }

        public string PickerTemplateName
        {
            get { return _PickerTemplateName; }        
        }

        public PickerSearchData(string lookupField, string pickerTemplateName)
        {
            _lookupField = lookupField;
            _PickerTemplateName = pickerTemplateName;
        }

        protected PickerSearchData(SerializationInfo info, StreamingContext context)
        {
            //_webID = (Guid)info.GetValue("WebID", typeof(Guid));
            _lookupField = info.GetString("LookupField");
            _PickerTemplateName = info.GetString("PickerTemplateName");
        }

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            //info.AddValue("WebID", _webID);
            info.AddValue("LookupField", _lookupField);
            info.AddValue("PickerTemplateName",_PickerTemplateName);
        }
        
        public PickerControlElement PickerTemplate
        {
            get {
                if(_defaultPickerTemplate == null)
                {
                    _defaultPickerTemplate = GetPickerTemplate(this.PickerTemplateName);
                }
                return _defaultPickerTemplate;
            }
        }
      
        internal static PickerControlElement GetPickerTemplate(string name)
        {
            SharePointSection gspSection = SharePointSection.GetSection();
            return gspSection.PickerControls[name];
        }

    }
}
